#pragma once
#include<iostream>
#include<pthread.h>
#include<string>
#include<functional>
#include<sys/syscall.h>
#include<unistd.h>

#define get_lwp_id() syscall(SYS_gettid)

const std::string initname="None-name";
using func_t=std::function<void(const std::string&name)>;

class Thread
{
private:
    static void*routine(void*args)
    {
        Thread*th=static_cast<Thread*>(args);
        th->_func(th->_name);
        th->_lwp=get_lwp_id();
        th->_isrunning=true;
        pthread_exit((void*)0);
    }

public:
    Thread(func_t func,const std::string&name=initname)
        :_func(func),_name(name),_isrunning(false)
    {
        std::cout<<"线程初始化成功"<<std::endl;
    }

    
    //启动线程
    void Start()
    {
        int n=pthread_create(&_tid,nullptr,routine,this);
        if(n==0)
            std::cout<<"启动线程："<<_name.c_str()<<"成功"<<std::endl;
    }
    //等待线程
    void Join()
    {
        if(!_isrunning)
            return;
        int n=pthread_join(_tid,nullptr);
        if(n==0)
            std::cout<<"线程等待成功"<<std::endl;
    }
    ~Thread(){}
private:
    pthread_t _tid;
    std::string _name;
    int _lwp;
    func_t _func;

    bool _isrunning;
};